package com.zb.controller;

import com.zb.dto.GenerateParam;
import com.zb.service.DataGeneratorService;
import com.zb.service.GeneratorStatusService;
import com.zb.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;

@Controller
public class DataGeneratorController {

    @Autowired
    private DataGeneratorService dataGeneratorService;

    @Autowired
    private LogService logService;

    @Autowired
    private GeneratorStatusService statusService;

    // 存储生成任务的Future
    private Future<Boolean> generateFuture;

    /**
     * 首页
     */
    @GetMapping("/")
    public String index(Model model) {
        model.addAttribute("generateParam", new GenerateParam());
        model.addAttribute("isGenerating", statusService.isGenerating());
        return "index";
    }

    /**
     * 开始生成数据
     */
    @PostMapping("/generate")
    @ResponseBody
    public Map<String, Object> generateData(@Valid @RequestBody GenerateParam param, BindingResult bindingResult) {
        Map<String, Object> result = new HashMap<>();

        // 验证参数
        if (bindingResult.hasErrors()) {
            result.put("success", false);
            result.put("message", bindingResult.getFieldError().getDefaultMessage());
            return result;
        }

        // 检查是否正在生成
        if (statusService.isGenerating()) {
            result.put("success", false);
            result.put("message", "数据生成已在进行中，请不要重复提交！");
            return result;
        }

        try {
            // 开始异步生成数据
            generateFuture = dataGeneratorService.generateOrderDataAsync(param);
            result.put("success", true);
            result.put("message", "数据生成已启动！");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "启动数据生成失败: " + e.getMessage());
        }

        return result;
    }

    /**
     * 获取最新日志
     */
    @GetMapping("/logs")
    @ResponseBody
    public Map<String, Object> getLatestLogs(@RequestParam(defaultValue = "100") int count) {
        Map<String, Object> result = new HashMap<>();
        List<String> logs = logService.getLatestLogs(count);
        result.put("logs", logs);
        result.put("isGenerating", statusService.isGenerating());
        return result;
    }

    /**
     * 清空日志
     */
    @PostMapping("/clearLogs")
    @ResponseBody
    public Map<String, Object> clearLogs() {
        Map<String, Object> result = new HashMap<>();
        try {
            logService.clearLogs();
            result.put("success", true);
            result.put("message", "日志已清空");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "清空日志失败: " + e.getMessage());
        }
        return result;
    }
}
